package com.jmx.yp.struts.login;

import java.util.*;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.providers.encoding.Md5PasswordEncoder;
import com.jmx.yp.entity.GroupEmployee;
import com.jmx.yp.exception.ErrorCode;
import com.jmx.yp.exception.HandlerException;
import com.jmx.yp.service.group.IGroupinfoService;
import com.jmx.yp.service.login.ILoginService;
import com.opensymphony.xwork2.ActionContext;
import javax.servlet.http.*;

/**
 * 
 * 功能描述：
 *			登录业务逻辑
 * @author Jenny
 *
 * Date:2011-1-9 下午06:52:35
 */
public class LoginAction {
	
	private static Logger log = (Logger)Logger.getLogger(LoginAction.class);
	
	private ILoginService loginService;
	
	private Md5PasswordEncoder md5;
	
	private IGroupinfoService groupService;
	
	public void setLoginService(ILoginService loginService){
		this.loginService = loginService;
	}

	public void setMd5(Md5PasswordEncoder md5) {
		this.md5 = md5;
	}
	
	public void setGroupService(IGroupinfoService groupService){
		this.groupService = groupService;
	}

	/**
	 * 登录入口
	 * @return
	 * @throws Exception
	 */
	public String init(){
		log.debug("init");
		return "init";
	}
	
	/**
	 * 登录
	 * @return
	 * @throws Exception
	 */
	public String login(){
		log.debug("login");
		String forward = "error";
		//System.out.println(ActionContext.getContext().getSession().get("securityCode"));
		try{
			GroupEmployee user = loginService.login(loginName, md5.encodePassword(pass, loginName));
			
			//如果该用户所属的机构或其上级组织机构中有被冻结的，用户都无法登录系统
			if(user.getGroup_srl() != null){
				if(groupService.isFreezed(user.getGroup_srl()))
					throw new HandlerException(ErrorCode.GROUP_ERROR);
			}
			
			ActionContext.getContext().getSession().put("user", user);
			
			//写cookie，防止IE后退
			HttpServletResponse response = ServletActionContext.getResponse(); 
			Cookie cookie = new Cookie("userId", "JMX:" + user.getGrpemp_id());
			cookie.setMaxAge(24*60*60);
			response.addCookie(cookie);
			
			forward = "login";
		}catch(HandlerException he){
			results = he.toString();
		}catch(Exception e){
			results = ErrorCode.COMMON_ERROR;
			e.printStackTrace();
		}
		
		return forward;
	}
	
	public String getHeader(){
		nowDate = new Date();
		return "header";
	}
	
	public String leftMenu(){
		log.debug("leftMenu");
		String forward = "error";

		try{
			//GroupEmployee user = loginService.login(loginName, pass);
			//ActionContext.getContext().getSession().put("user", user);
			//构造菜单
			menuList = loginService.getMenu();
			forward = "menu";
		}catch(HandlerException he){
			results = he.toString();
		}catch(Exception e){
			results = ErrorCode.COMMON_ERROR;
			e.printStackTrace();
		}
		return forward;
	}
	
	/**
	 * 退出
	 * @return
	 */
	public String logout(){
		HttpServletRequest request = ServletActionContext.getRequest(); 
		HttpServletResponse response = ServletActionContext.getResponse(); 
		HttpSession session = request.getSession(); 
			//注销session 
		session.removeAttribute("logName"); 
		session.invalidate(); 
		try{ 
			                           //清除页面缓存 
			response.setHeader("Pragma","No-cache");  
			response.setHeader("Cache-Control","no-cache"); 
			response.setHeader("Cache-Control", "no-store"); 
			response.setDateHeader("Expires", 0); 

			//response.sendRedirect("/");//这句出错
			
			//更新cookie，避免IE后退按钮
			Cookie cookie = new Cookie("userId", "-1000");
			cookie.setMaxAge(24*60*60);
			response.addCookie(cookie);

		}catch(Exception e){ 
			e.printStackTrace(); 
		} 
		return "logout"; 
	} 

	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public String getResults() {
		return results;
	}
	public void setResults(String results) {
		this.results = results;
	}
	public List getMenuList() {
		return menuList;
	}
	public void setMenuList(List menuList) {
		this.menuList = menuList;
	}
	public void setNowDate(Date date){
		this.nowDate = date;
	}
	public Date getNowDate(){
		return nowDate;
	}

	private String loginName;
	private String pass;
	private String results;
	
	private List menuList;	
	private Date nowDate;
	
	public static void main(String args[]){
		Md5PasswordEncoder md5 = new Md5PasswordEncoder();
		System.out.println(md5.encodePassword("123", "admin"));
	}
}
